<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use App\Http\Resources\AdResource;
use App\Models\Ad;
use App\Models\UserVisitLog;
class UserVisitLogController extends Controller
{
    public function show(Request $request,UserVisitLog $userVisitLog){
        $today = Carbon::today()->toDateString();
        $count = $userVisitLog->where("visit_date","=",$today)->where("ip_addr","=",$request->ip())->count();
        //今天看过网站了
        if ($count>0){
            $status = [
                "status"=>-1,
                "msg"  =>"今日已展示",
            ];
            return response()->json($status)->setStatusCode(200);
        }else{
            $userVisitLog->create([
                "visit_date"=>$today,
                "ip_addr" => $request->ip(),
                "user_id"=>auth('api')->user()?auth('api')->user()->id:0,
            ]);
            $ad = Ad::where("pid","7")->where("enabled",true)->firstOrFail();
            return new AdResource($ad);
        }

    }
}
